const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
//引入数据库模块
const db = require('./db/index');
//引入路由器模块
const Register = require('./router/register');
const Login = require('./router/login');
const Student_info = require('./router/student_info');
const Entry_exam = require('./router/entry_exam');
const Mid_exam = require('./router/mid_exam');
const End_exam = require('./router/end_exam');
const Search_info = require('./router/search_info');

const app = express();
//暴露文件
app.use(express.static('public'));
//body-parser包
app.use(bodyParser.urlencoded({extended:true}));
//解决跨域
app.use(cors());
//全局中间件处理，确保数据库连接成功
app.use('/',async(req,res,next)=>{
    try{
        await db;
        next();
    }catch(e){
        console.log(e);
        res.send('服务器和数据库连接失败！')
    }
})

//应用路由器模块
app.use(Register);   //注册路由
app.use(Login);      //登录路由
app.use(Student_info);  //学生信息及三级联动
app.use(Entry_exam);   //入学考试
app.use(Mid_exam);     //期中考试
app.use(End_exam);     //期末考试
app.use(Search_info);   //查询带头像的学生信息

app.listen(3000,err=>{
    if(!err) console.log('服务器启动成功！');
    else console.log(err);
})